<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/*----------------------------------------------------------------------------
<span class='line'>  2</span>  | Copyright (c) 2009 Matthew B. Brophy (matt@brophy.org).  
<span class='line'>  3</span>  | Liscense: MIT License (http://www.opensource.org/licenses/mit-license.php)
<span class='line'>  4</span>  |
<span class='line'>  5</span>  | $Id: D__matt_Computer_JavaScript_orgbrophyjs_trunk_org_brophy_js_array.js.html 30 2009-07-31 03:13:23Z matt@brophy.org $
<span class='line'>  6</span>  -----------------------------------------------------------------------------*/</span><span class="WHIT">
<span class='line'>  7</span> 
<span class='line'>  8</span> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">org</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">"undefined"</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'>  9</span> </span><span class="WHIT">    </span><span class="NAME">org</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="NAME">org</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 10</span> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">org.brophy</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">"undefined"</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 11</span> </span><span class="WHIT">    </span><span class="NAME">org.brophy</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="NAME">org.brophy</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 12</span> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">typeof</span><span class="WHIT"> </span><span class="NAME">org.brophy.js</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">"undefined"</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 13</span> </span><span class="WHIT">    </span><span class="NAME">org.brophy.js</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="NAME">org.brophy.js</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 14</span> 
<span class='line'> 15</span> </span><span class="COMM">/** 
<span class='line'> 16</span>  * @namespace 
<span class='line'> 17</span>  * @description Library of useful functions for dealing with arrays in 
<span class='line'> 18</span>  *              HTML pages.
<span class='line'> 19</span>  * @requires    This library requires the &lt;a href="http://www.mochikit.com">
<span class='line'> 20</span>  *              MochiKit&lt;/a> JavaScript library 
<span class='line'> 21</span>  *              (version 1.4+).
<span class='line'> 22</span>  */</span><span class="WHIT">
<span class='line'> 23</span> </span><span class="NAME">org.brophy.js.array</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 24</span> 
<span class='line'> 25</span> </span><span class="COMM">/**
<span class='line'> 26</span>  * Finds the index of a value 'val' in an array 'arr'.
<span class='line'> 27</span>  *
<span class='line'> 28</span>  * @param    arr  
<span class='line'> 29</span>  *           The array to search
<span class='line'> 30</span>  * @param    val  
<span class='line'> 31</span>  *           The value to look for
<span class='line'> 32</span>  * @param    [cmpFunc]
<span class='line'> 33</span>  *           A function following the MochiKit.Base.compare function syntax 
<span class='line'> 34</span>  *           for comparing two items and returning 0 if equal.  If no 
<span class='line'> 35</span>  *           function is passed, then the MochiKit.Base.compare() function 
<span class='line'> 36</span>  *           will be used.
<span class='line'> 37</span>  * @return   The index in the array, otherwise -1
<span class='line'> 38</span>  * @type     number
<span class='line'> 39</span>  */</span><span class="WHIT">
<span class='line'> 40</span> </span><span class="NAME">org.brophy.js.array.indexOf</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">,</span><span class="NAME">val</span><span class="PUNC">,</span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 41</span> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 42</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="PUNC">(</span><span class="NAME">MochiKit.Base.isUndefinedOrNull</span><span class="PUNC">(</span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 43</span> </span><span class="WHIT">  </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 44</span> </span><span class="WHIT">    </span><span class="NAME">cmpFunc</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MochiKit.Base.compare</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 45</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 46</span> 
<span class='line'> 47</span> </span><span class="WHIT">  </span><span class="KEYW">for</span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">arr.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 48</span> </span><span class="WHIT">  </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 49</span> </span><span class="WHIT">    </span><span class="KEYW">try</span><span class="WHIT">
<span class='line'> 50</span> </span><span class="WHIT">    </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 51</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="PUNC">(</span><span class="NAME">cmpFunc</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="NAME">val</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 52</span> </span><span class="WHIT">        </span><span class="PUNC">{</span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">;</span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 53</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 54</span> </span><span class="WHIT">    </span><span class="KEYW">catch</span><span class="PUNC">(</span><span class="NAME">typeError</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 55</span> </span><span class="WHIT">    </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 56</span> </span><span class="WHIT">      </span><span class="COMM">/* Fail silently...That was obviously not the right element 
<span class='line'> 57</span>          in the array... */</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 58</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 59</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 62</span> 
<span class='line'> 63</span> </span><span class="COMM">/**
<span class='line'> 64</span>  * @description  Sorts the array 'arr' in whatever fashion you desire.
<span class='line'> 65</span>  *
<span class='line'> 66</span>  * @param    arr
<span class='line'> 67</span>  *           The array to be sorted
<span class='line'> 68</span>  *
<span class='line'> 69</span>  * @param    [prop] 
<span class='line'> 70</span>  *           Assuming 'arr' is an array of objects, 'prop' identifies the 
<span class='line'> 71</span>  *           property to sort on.  If 'prop' is not passed, then the array
<span class='line'> 72</span>  *           is assumed to be an array of strings and is sorted as such.
<span class='line'> 73</span>  *
<span class='line'> 74</span>  * @param    [cmpFunc]
<span class='line'> 75</span>  *           A function following the MochiKit.Base.compare function syntax 
<span class='line'> 76</span>  *           for comparing two items and returning 1, 0, or -1.  If no 
<span class='line'> 77</span>  *           function is passed, then the MochiKit.Base.compare() function 
<span class='line'> 78</span>  *           will be used.
<span class='line'> 79</span>  *
<span class='line'> 80</span>  * @param    [ignore_n = 0]
<span class='line'> 81</span>  *           Identified the number of elements at the beginning of the array to 
<span class='line'> 82</span>  *           ignore during the sort.
<span class='line'> 83</span>  *
<span class='line'> 84</span>  * @return   The sorted array.
<span class='line'> 85</span>  * @type     array
<span class='line'> 86</span>  */</span><span class="WHIT">
<span class='line'> 87</span> </span><span class="NAME">org.brophy.js.array.sortArrayOfObjects</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">prop</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cmpFunc</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">ignore_n</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 88</span> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 89</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="PUNC">(</span><span class="NAME">MochiKit.Base.isUndefinedOrNull</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 90</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="PUNC">(</span><span class="NAME">MochiKit.Base.isUndefinedOrNull</span><span class="PUNC">(</span><span class="NAME">ignore_n</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="NAME">ignore_n</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 91</span> 
<span class='line'> 92</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">MochiKit.Base.isUndefinedOrNull</span><span class="PUNC">(</span><span class="NAME">prop</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 93</span> </span><span class="WHIT">      </span><span class="NAME">MochiKit.Base.isUndefinedOrNull</span><span class="PUNC">(</span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 94</span> </span><span class="WHIT">  </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 95</span> </span><span class="WHIT">    </span><span class="NAME">cmpFunc</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">a</span><span class="PUNC">,</span><span class="NAME">b</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'> 96</span> </span><span class="WHIT">                </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">MochiKit.Base.compare</span><span class="PUNC">(</span><span class="NAME">a</span><span class="PUNC">[</span><span class="NAME">prop</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">b</span><span class="PUNC">[</span><span class="NAME">prop</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 97</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 98</span> </span><span class="WHIT">  </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="KEYW">if</span><span class="PUNC">(</span><span class="NAME">MochiKit.Base.isUndefinedOrNull</span><span class="PUNC">(</span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'> 99</span> </span><span class="WHIT">  </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'>100</span> </span><span class="WHIT">    </span><span class="NAME">cmpFunc</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MochiKit.Base.compare</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'>101</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>102</span> </span><span class="WHIT">  </span><span class="WHIT">
<span class='line'>103</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">ignoreElems</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>104</span> </span><span class="WHIT">  </span><span class="KEYW">for</span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">ignore_n</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'>105</span> </span><span class="WHIT">  </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>106</span> </span><span class="WHIT">    </span><span class="NAME">ignoreElems.push</span><span class="PUNC">(</span><span class="NAME">arr.shift</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>107</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'>108</span> </span><span class="WHIT">  </span><span class="WHIT">
<span class='line'>109</span> </span><span class="WHIT">  </span><span class="NAME">arr</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MochiKit.Iter.sorted</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>110</span> 
<span class='line'>111</span> </span><span class="WHIT">  </span><span class="KEYW">for</span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">ignore_n</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'>112</span> </span><span class="WHIT">  </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>113</span> </span><span class="WHIT">    </span><span class="NAME">arr.unshift</span><span class="PUNC">(</span><span class="NAME">ignoreElems.pop</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>114</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="WHIT">
<span class='line'>115</span> </span><span class="WHIT">  </span><span class="WHIT">
<span class='line'>116</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">arr</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>117</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>118</span> 
<span class='line'>119</span> </span><span class="COMM">/**
<span class='line'>120</span>  * @description  Sorts the array 'arr' 
<span class='line'>121</span>  *
<span class='line'>122</span>  * @param    arr
<span class='line'>123</span>  *           The array to be sorted
<span class='line'>124</span>  *
<span class='line'>125</span>  * @param    [cmpFunc]
<span class='line'>126</span>  *           A function following the MochiKit.Base.compare function syntax 
<span class='line'>127</span>  *           for comparing two items and returning 1, 0, or -1.  If no 
<span class='line'>128</span>  *           function is passed, then the MochiKit.Base.compare() function 
<span class='line'>129</span>  *           will be used.
<span class='line'>130</span>  *
<span class='line'>131</span>  * @return   The sorted array.
<span class='line'>132</span>  * @type     array
<span class='line'>133</span>  */</span><span class="WHIT">
<span class='line'>134</span> </span><span class="NAME">org.brophy.js.array.sortArray</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">,</span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'>135</span> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>136</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">org.brophy.js.array.sortArrayOfObjects</span><span class="PUNC">(</span><span class="NAME">arr</span><span class="PUNC">,</span><span class="KEYW">null</span><span class="PUNC">,</span><span class="NAME">cmpFunc</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>137</span> </span><span class="PUNC">}</span></pre></body></html>